package comgutianyu.qihangjiaoyu.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import comgutianyu.qihangjiaoyu.model.User;

public class UserDao {
    private DatabaseHelper dbHelper;
    private SQLiteDatabase db;

    public UserDao(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    // 添加用户
    public long insertUser(User user) {
        db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_NICKNAME, user.getNickname());
        values.put(DatabaseHelper.COLUMN_USERNAME, user.getUsername());
        values.put(DatabaseHelper.COLUMN_PHONE, user.getPhone());
        values.put(DatabaseHelper.COLUMN_GENDER, user.getGender());
        values.put(DatabaseHelper.COLUMN_EXAM_INTENTION, user.getExamIntention());
        values.put(DatabaseHelper.COLUMN_AVATAR_PATH, user.getAvatarPath());

        long id = db.insert(DatabaseHelper.TABLE_USERS, null, values);
        db.close();
        return id;
    }

    // 更新���信息
    public int updateUser(User user) {
        db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_NICKNAME, user.getNickname());
        values.put(DatabaseHelper.COLUMN_PHONE, user.getPhone());
        values.put(DatabaseHelper.COLUMN_GENDER, user.getGender());
        values.put(DatabaseHelper.COLUMN_EXAM_INTENTION, user.getExamIntention());
        values.put(DatabaseHelper.COLUMN_AVATAR_PATH, user.getAvatarPath());

        int rowsAffected = db.update(DatabaseHelper.TABLE_USERS, values,
                DatabaseHelper.COLUMN_USERNAME + " = ?",
                new String[]{user.getUsername()});
        db.close();
        return rowsAffected;
    }

    // 根据用户名获取用户信息
    public User getUserByUsername(String username) {
        db = dbHelper.getReadableDatabase();
        User user = null;

        Cursor cursor = db.query(DatabaseHelper.TABLE_USERS, null,
                DatabaseHelper.COLUMN_USERNAME + " = ?",
                new String[]{username}, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID)));
            user.setNickname(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NICKNAME)));
            user.setUsername(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_USERNAME)));
            user.setPhone(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_PHONE)));
            user.setGender(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_GENDER)));
            user.setExamIntention(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_EXAM_INTENTION)));
            user.setAvatarPath(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_AVATAR_PATH)));
            cursor.close();
        }
        db.close();
        return user;
    }

    // 删除用户
    public int deleteUser(String username) {
        db = dbHelper.getWritableDatabase();
        int rowsAffected = db.delete(DatabaseHelper.TABLE_USERS,
                DatabaseHelper.COLUMN_USERNAME + " = ?",
                new String[]{username});
        db.close();
        return rowsAffected;
    }

    // 检查用户名是否存在
    public boolean isUsernameExists(String username) {
        db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DatabaseHelper.TABLE_USERS,
                new String[]{DatabaseHelper.COLUMN_USERNAME},
                DatabaseHelper.COLUMN_USERNAME + " = ?",
                new String[]{username},
                null, null, null);
        
        boolean exists = cursor != null && cursor.getCount() > 0;
        
        if (cursor != null) {
            cursor.close();
        }
        db.close();
        return exists;
    }

    // 根据手机号获取用户信息
    public User getUserByPhone(String phone) {
        db = dbHelper.getReadableDatabase();
        User user = null;

        Cursor cursor = db.query(DatabaseHelper.TABLE_USERS, null,
                DatabaseHelper.COLUMN_PHONE + " = ?",
                new String[]{phone}, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID)));
            user.setNickname(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NICKNAME)));
            user.setUsername(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_USERNAME)));
            user.setPhone(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_PHONE)));
            user.setPassword(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_PASSWORD)));
            user.setGender(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_GENDER)));
            user.setExamIntention(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_EXAM_INTENTION)));
            user.setAvatarPath(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_AVATAR_PATH)));
            cursor.close();
        }
        db.close();
        return user;
    }
} 